package com.yql.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.yql.entity.Bom;
import com.yql.entity.BomVO;
import com.yql.entity.Bommx;
import com.yql.entity.User;
import com.yql.service.BomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BomController {
    @Autowired
    BomService bomService;
    @RequestMapping("list")
    public String list(BomVO vo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "1") Integer pageSize, HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        vo.setUser(user);
        PageInfo<Bom> info = bomService.listBom(vo, pageNum, pageSize);
        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        model.addAttribute("uname",user.getUname());
        return "list";
    }

    @GetMapping("toAdd")
    public String toAdd(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        model.addAttribute("username",user.getUname());
        Snowflake snowflake = IdUtil.getSnowflake(10, 10);
        Long l = snowflake.nextId();
        model.addAttribute("l",l);
        List<Bommx> list1 = bomService.listBommx();
        model.addAttribute("list",list1);
        return "add";
    }

    @ResponseBody
    @PostMapping("deleteById")
    public boolean deleteById(Integer id){
        Integer i = bomService.deleteById(id);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }
}
